<?php

namespace app\index\controller;

use think\facade\Db;
use think\facade\View;
use think\facade\Request;

// class IShop extends Base
class IShop extends Base
{
    # 商品分类
    public function goodsTypeLists()
    {
        $lists = Db::table('app_shop_goods_type')->where([["is_del", '=', 1], ['status', '=', 1]])->select()->toArray();
        $this->returnCode(0, $lists);
    }

    # 根据分类id 查询该类别下所有商品
    public function getGoodsByTypeId()
    {
        $type_id = trim(input('post.type_id'));
        $lists = Db::table('app_shop_goods_info')->where([
            ['is_del', '=', 1],
            ['type_id', '=', $type_id],
            ['status', '=', 1]
        ])->select()->toArray();

        // 处理数据 返回给前端显示
        foreach ($lists as &$lists_v) {
            $lists_v['status_s'] = $lists_v['status'] == 1 ? '上架' : '下架';
            $lists_v['is_top_s'] = $lists_v['is_top'] == 1 ? '默认' : '置顶';
            $lists_v['time_add'] = date('Y年m月d日 H:m:s', $lists_v['time_add']);
            $lists_v['time_update'] = $lists_v['time_update'] == null ? '' : date('Y年m月d日 H:m:s', $lists_v['time_update']);
            // $lists_v['img_s'] = [];
            // 遍历数组 并将图片路径设置为绝对路径 方便前端展示
            if (!empty($lists_v['img'])) {
                // 将字符串以 ；符号分割为数组
                $img =  explode(';', $lists_v['img']);
                // 弹出数组中最后一个元素 因为以 ；分隔为数组后最后一个元素为空值
                array_pop($img);
                foreach ($img as $k => $img_v) {
                    if (!empty($img_v)) {
                        $lists_v['img_s'][] = [
                            'uid' => $k,
                            'url' => $this->domain_name . $img_v,
                            'data' => $img_v
                        ];
                    }
                }
            }
        }
        $this->returnCode(0, $lists);
    }

    # 根据分类id 查询该类别下所有商品
    public function getGoodsById()
    {
        $gid = trim(input('post.gid'));
        $lists = Db::table('app_shop_goods_info')->where('gid', $gid)->find();
        $lists['status_s'] = $lists['status'] == 1 ? '上架' : '下架';
        $lists['is_top_s'] = $lists['is_top'] == 1 ? '默认' : '置顶';
        $lists['time_add'] = date('Y年m月d日 H:m:s', $lists['time_add']);
        $lists['time_update'] = $lists['time_update'] == null ? '' : date('Y年m月d日 H:m:s', $lists['time_update']);
        if (!empty($lists['img'])) {
            // 将字符串以 ；符号分割为数组
            $img =  explode(';', $lists['img']);
            // 弹出数组中最后一个元素 因为以 ；分隔为数组后最后一个元素为空值
            array_pop($img);
            foreach ($img as $k => $img_v) {
                if (!empty($img_v)) {
                    $lists['img_s'][] = [
                        'uid' => $k,
                        'url' => $this->domain_name . $img_v,
                        'data' => $img_v
                    ];
                }
            }
        }
        $this->returnCode(0, $lists);
    }

    # 创建订单
    public function createOrder()
    {
        // 生成订单数据
        $data['gid'] = trim(input('post.gid'));
        $data['uid'] = trim(input('post.uid'));
        $data['aid'] = trim(input('post.aid'));
        $data['ooid'] = 'SHOP' .  date('YmdHis', time()) . mt_rand(10000, 99999);
        $data['pay_time']  = time();
        $data['amount'] = trim(input('post.amount'));
        $data['status'] = 2;
        $data['pay_type'] = 0;

        // 生成余额记录表
        $balanceLogData['uid'] = trim(input('post.uid'));
        // 消费记录负值
        $balanceLogData['money'] = -trim(input('post.amount'));
        $balanceLogData['before'] = trim(input('post.balance'));
        // 因为消费是记录负值 所有这里需用 + 号 来更新余额记录
        $balanceLogData['after'] = $balanceLogData['before'] + $balanceLogData['money'];
        $balanceLogData['desc'] = '消费' . trim(input('post.amount'));;
        $balanceLogData['type'] = 3;
        $balanceLogData['createtime'] = time();

        // 添加 余额变动记录
        $resBalanceLog = Db::table('app_user_balance_log')->insert($balanceLogData);
        if (!$resBalanceLog) {
            $this->returnCode(81000007);
        }

        // 更新余额
        $updateBalance['balance'] = $balanceLogData['after'];
        $updateRes =  Db::table('app_user')->where('uid', $data['uid'])->update($updateBalance);
        if (!$resBalanceLog) {
            $this->returnCode(81000008);
        }

        // 生成订单
        $res = Db::table('app_shop_order')->insert($data);
        if (!$res) {
            $this->returnCode(81000009);
        }
        $this->returnCode(0);
    }
}
